package com.sixfoot.handler;

import com.sixfoot.hrv.codec.protobuf.GetResourceListReqProto;
import com.sixfoot.hrv.codec.protobuf.LoginReqProto;
import com.sixfoot.hrv.codec.protobuf.LoginRespProto;
import com.sixfoot.hrv.codec.protobuf.RequestProto;
import com.sixfoot.hrv.codec.protobuf.RequestProto.Request;
import com.sixfoot.hrv.codec.protobuf.ResourceListRespProto;
import com.sixfoot.hrv.codec.protobuf.ResourceProto.Resource;
import com.sixfoot.hrv.codec.protobuf.ResponseProto;
import com.sixfoot.hrv.codec.protobuf.SaveScaleRespProto;
import com.sixfoot.hrv.codec.protobuf.ScaleListRespProto;
import com.sixfoot.hrv.codec.protobuf.ScaleRespProto;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class LoginRespClientHandler extends ChannelInboundHandlerAdapter {
//	final Logger logger = LoggerFactory.getLogger(LoginRespClientHandler.class);
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) {
		ctx.write(subRequest());
		ctx.flush();
	}
	
	private RequestProto.Request subRequest() {
		LoginReqProto.LoginReq login = LoginReqProto.LoginReq.newBuilder()
				.setUsername("zhangsan")
				.setPassword("123456")
				.build();
		
		RequestProto.Request.Builder builder = RequestProto.Request.newBuilder();
		builder.setType(Request.Type.LOGIN);
		builder.setExtension(LoginReqProto.LoginReq.loginReq, login);
		return builder.build();
	}


	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ResponseProto.Response response = (ResponseProto.Response) msg;
		
		// 判断返回值是OK或ERROR
		switch (response.getStatus().getNumber()) {
			case (ResponseProto.Response.Status.OK_VALUE):
				ResponseProto.RespSuccess respSuccess = response.getExtension(ResponseProto.RespSuccess.respSuccess);
				// 判断返回对象是哪种类型
				switch (respSuccess.getType().getNumber()) {
					case (ResponseProto.RespSuccess.Type.LOGIN_VALUE):
						LoginRespProto.LoginResp loginResp = respSuccess.getExtension(LoginRespProto.LoginResp.loginResp);
//						ChangePasswordReqProto.ChangePasswordReq changePassword = 
//								ChangePasswordReqProto.ChangePasswordReq.newBuilder()
//								.setUsername("zhangsan")
//								.setOldPassword("123456")
//								.setNewPassword("123")
//								.build();
//						
//						RequestProto.Request.Builder builder = RequestProto.Request.newBuilder();
//						builder.setType(Request.Type.CHANGE_PASSWORD);
//						builder.setExtension(ChangePasswordReqProto.ChangePasswordReq.changePasswordReq, changePassword);
//						ctx.writeAndFlush(builder.build());
						
						break;
					case (ResponseProto.RespSuccess.Type.CHANGE_PASSWORD_VALUE):
						break;
					case (ResponseProto.RespSuccess.Type.RESOURCE_LIST_VALUE):
						ResourceListRespProto.ResourceListResp resourceListResp 
							= respSuccess.getExtension(ResourceListRespProto.ResourceListResp.resourceListResp);
						for (Resource resource : resourceListResp.getResourceList()) {
							System.out.println("name:" + resource.getName() + "; ids:" + resource.getParentIds() + "; enabled:" + resource.getIsEnabled());
						}
						break;
					case (ResponseProto.RespSuccess.Type.SAVE_SCALE_VALUE):
						SaveScaleRespProto.SaveScaleResp saveScaleResp 
							= respSuccess.getExtension(SaveScaleRespProto.SaveScaleResp.saveScaleResp);
						System.out.println(saveScaleResp.getResult());
						break;
					case (ResponseProto.RespSuccess.Type.GET_SCALE_LIST_VALUE):
						ScaleListRespProto.ScaleListResp scaleListResp
							= respSuccess.getExtension(ScaleListRespProto.ScaleListResp.scaleListResp);
						System.out.println(scaleListResp);
						break;
					case (ResponseProto.RespSuccess.Type.GET_SCALE_VALUE):
						ScaleRespProto.ScaleResp scaleResp = respSuccess.getExtension(ScaleRespProto.ScaleResp.scaleResp);
						System.out.println(scaleResp);
						break;
					default:
						break;
				}
				break;
			case (ResponseProto.Response.Status.ERROR_VALUE):
				ResponseProto.RespFailed respFailed = response.getExtension(ResponseProto.RespFailed.respfailed);
				System.out.println(respFailed.getErrorDescription());
				break;
			default:
				break;
		}
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		cause.printStackTrace();
		ctx.close();
	}

}
